$(function(){
    initKhgl();
    initGcmc();
})
//加载树形
function initKhgl() {
    //请求请求
   /* $.get("/statics/tree.json",null,function(data){
        console.log(data)
        $('#khgl_tree').tree({
            data: [data],
            loadFilter: function(data){
                if (data.d){
                    return data.d;
                } else {
                    return data;
                }
            },
            onSelect:function(node){
                alert(node.id+","+node.text)
            }
    })*/
    alert(2)
        //动态后台请求
    $('#khgl_tree').tree({
        url: "/khmc/getAll",
        animate:true,
        loadFilter:function(data){
            return convertTreeJson(data);
        },
        onSelect:function(node){
            alert(node.id+","+node.text)
        }

});
}
//转换属性json格式
function convertTreeJson(rows){
    var treeJson = [];//树形菜单数据
    //得到根节点
    for(var i=0;i<rows.length;i++){
        var row = rows[i];//每一个节点
        if(!exists(rows,row.parentId)){
            treeJson.push({"id":row.id,"text":row.text,"state":row.state});
        }
    }
    //操作children
    var parentJson = [];
    for(var i = 0;i<treeJson.length;i++){
        parentJson.push(treeJson[i]);
    }
    while(parentJson.length){
        var sonJson = parentJson.shift();// 得到父节点
        //得到子节点
        for(var i=0;i<rows.length;i++){
            var row = rows[i];
            if(row.parentId == sonJson.id){
                var child = {id:row.id,text:row.text};
                if(sonJson.children){
                    sonJson.children.push(child);
                }else{//第一次添加子节点
                    sonJson.children = [child];
                }
                parentJson.push(child);
            }
        }
    }
    return treeJson;
}
//通过判断id是否和父id相同
function exists(rows,parentId){
    for(var i=0;i<rows.length;i++){
        if(rows[i].id == parentId){
            return true;
        }
    }
    return false;
}

//加载工程信息
function initGcmc(){
    $('#khgl_gcmc').datagrid({
        //url:"",
        data:[{"gcmc_id":2001,"gcmc_name":"一食堂","khmc_id":1001,"khmc_name":"用户1"},
            {"gcmc_id":2002,"gcmc_name":"二食堂","khmc_id":1001,"khmc_name":"用户1"},
            {"gcmc_id":2003,"gcmc_name":"三食堂","khmc_id":1001,"khmc_name":"用户1"},
            {"gcmc_id":2004,"gcmc_name":"一学校","khmc_id":1002,"khmc_name":"用户2"}],
        columns:[[
            {field:'checkbox',title:'全选',checkbox:true},
            {field:'gcmc_id',title:'工程ID',width:100},
            {field:'gcmc_name',title:'工程name',width:100},
            {field:'khmc_id',title:'客户ID',width:100},
            {field:'khmc_name',title:'客户名称',width:100},
            {field:'opr',title:'操作',width:100,formatter:function(value,row,index){
                    var str = "<button onclick='alert("+row.gcmc_id+")'>删除</button>";
                    return str;
                }}
        ]],
        fitColumns:true,
        pagination:true,
        toolbar: [{
            iconCls: 'icon-add',
            handler: function(){alert('新增')}
        },'-',{
            iconCls: 'icon-edit',
            handler: function(){alert('编辑')}
        },'-',{
            iconCls: 'icon-delete',
            handler: function(){alert('删除')}
        }],
        rownumbers:true,
        height:600
    });
}